﻿Public Class TimePicker

Private HoraActual    As String 
Private MinutoActual  As String 
Private SegundoActual As String 
    Private Foco As String

Public ReadOnly Property Hora() As String
    Get
       Return Convert.ToInt16(txtHora.Text)
    End Get    
End Property

Public ReadOnly Property Minuto() As String
    Get
       Return Convert.ToInt16(txtMinuto.Text)
    End Get    
End Property
    
Public ReadOnly Property Segundo() As String
    Get
       Return Convert.ToInt16(txtSegundo.Text)
    End Get    
End Property


Public ReadOnly Property HoraCompleta() As String 
    Get
        Return  txtHora.Text & ":" & txtMinuto.Text & ":"  & txtSegundo.Text
    End Get
End Property


    Private Sub imgArriba_MouseLeftButtonUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles imgArriba.MouseLeftButtonUp
        Try
            Select Case Foco
                Case "Minuto"                    
                    If Me.txtMinuto.Text.Trim.Equals("") Then
                        Me.txtMinuto.Text = "00"
                    Else
                        If Convert.ToInt16(Me.txtMinuto.Text) = 59 Then
                            Me.txtMinuto.Text = "00"
                        Else
                            Me.txtMinuto.Text = (Convert.ToInt16(Me.txtMinuto.Text) + 1).ToString("0#")
                        End If
                    End If
                Case "Segundo"                    
                    If Me.txtSegundo.Text.Trim.Equals("") Then
                        Me.txtSegundo.Text = "00"
                    Else
                        If Convert.ToInt16(Me.txtSegundo.Text) = 59 Then
                            Me.txtSegundo.Text = "00"
                        Else
                            Me.txtSegundo.Text = (Convert.ToInt16(Me.txtSegundo.Text) + 1).ToString("0#")
                        End If
                    End If
                Case Else                    
                    Me.txtHora.Focus()
                    If Me.txtHora.Text.Trim.Equals("") Then
                        Me.txtHora.Text = "00"
                    Else
                        If Convert.ToInt16(Me.txtHora.Text) = 23 Then
                            Me.txtHora.Text = "00"
                        Else
                            Me.txtHora.Text = (Convert.ToInt16(Me.txtHora.Text) + 1).ToString("0#")
                        End If
                    End If                    
            End Select
        Catch ex As Exception
        End Try
    End Sub

    Private Sub imgAbajo_MouseLeftButtonUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles imgAbajo.MouseLeftButtonUp
        Try
            Select Case Foco
                Case "Minuto"
                    If Convert.ToInt16(Me.txtMinuto.Text) = 0 Then
                        Me.txtMinuto.Text = "59"
                    Else
                        Me.txtMinuto.Text = (Convert.ToInt16(Me.txtMinuto.Text) - 1).ToString("0#")
                    End If
                Case "Segundo"
                    If Convert.ToInt16(Me.txtSegundo.Text) = 0 Then
                        Me.txtSegundo.Text = "59"
                    Else
                        Me.txtSegundo.Text = (Convert.ToInt16(Me.txtSegundo.Text) - 1).ToString("0#")
                    End If
                Case Else
                    Me.txtHora.Focus()
                    If Convert.ToInt16(Me.txtHora.Text) = 0 Then
                        Me.txtHora.Text = "23"
                    Else
                        Me.txtHora.Text = (Convert.ToInt16(Me.txtHora.Text) - 1).ToString("0#")
                    End If
            End Select
        Catch ex As Exception
        End Try
    End Sub

    Private Sub ObtenerFoco(sender As Object, e As RoutedEventArgs)
        Select Case TryCast(e.Source, TextBox).Name

            Case "txtHora"
                HoraActual = txtHora.Text
                Foco = "Hora"
            Case "txtMinuto"
                MinutoActual = txtMinuto.Text
                Foco = "Minuto"
            Case "txtSegundo"
                SegundoActual = txtSegundo.Text
                Foco = "Segundo"
        End Select
    End Sub

Private Sub PerderFoco(sender As Object, e As RoutedEventArgs)

Dim aux As String = "00"


Select case  TryCast ( e.Source,TextBox).Name 
     Case "txtHora"
           aux = HoraActual  
     Case "txtMinuto"
           aux = MinutoActual
     Case "txtSegundo"
           aux = SegundoActual  
 End Select


 If not IsNumeric (TryCast( e.Source,TextBox).Text ) then   
    TryCast( e.Source,TextBox).Text =  String.Format(aux,"00")    
 Else
    TryCast( e.Source,TextBox).Text =  Convert.ToInt16 ( TryCast( e.Source,TextBox).Text).ToString ("00")
 End If



End Sub

Private Sub PrevioTeclaAbajo(ByVal sender As System.Object , ByVal e As System.Windows.Input.KeyEventArgs)


  If  e.Key = Key.Delete  or e.Key = Key.Back  then           
      e.Handled = false
  ElseIf e.Key = Key.Space     then 
      e.Handled = True 
  End If


End Sub

Private Sub PrevioTextoAbajo(ByVal sender As System.Object , ByVal e As System.Windows.Input.TextCompositionEventArgs )

  Dim aux As String 


  If not IsNumeric (e.Text ) then 
     e.Handled = true
  Else

     If Not TryCast( e.Source,TextBox).Text.Trim.Equals ("") then
 
         If  TryCast( e.Source,TextBox).SelectionStart = 0 then 
             aux = e.Text &  Convert.ToInt16 (TryCast( e.Source,TextBox).Text)
         else
             aux = Convert.ToInt16 (TryCast( e.Source,TextBox).Text) & e.Text              
         End If

         
         Select case  TryCast ( e.Source,TextBox).Name 
             Case "txtHora"
                   If Convert.ToInt16 (aux) < 0 or  Convert.ToInt16 (aux) > 23 then       
                       e.Handled = true    
                   End If       
             Case "txtMinuto", "txtSegundo"
                   If Convert.ToInt16 (aux) < 0 or  Convert.ToInt16 (aux) > 59 then       
                       e.Handled = true    
                   End If                               
 
         End Select
                
     End If

  End If



End Sub




Public Sub New()
                
        InitializeComponent()        
        

        AddHandler Me.txtHora.GotFocus, AddressOf ObtenerFoco
        AddHandler Me.txtMinuto.GotFocus, AddressOf ObtenerFoco
        AddHandler Me.txtSegundo.GotFocus, AddressOf ObtenerFoco

        AddHandler Me.txtHora.LostFocus, AddressOf PerderFoco
        AddHandler Me.txtMinuto.LostFocus, AddressOf PerderFoco
        AddHandler Me.txtSegundo.LostFocus, AddressOf PerderFoco

        AddHandler Me.txtHora.PreviewKeyDown, AddressOf PrevioTeclaAbajo 
        AddHandler Me.txtMinuto.PreviewKeyDown, AddressOf PrevioTeclaAbajo
        AddHandler Me.txtSegundo.PreviewKeyDown, AddressOf PrevioTeclaAbajo


        AddHandler Me.txtHora.PreviewTextInput, AddressOf PrevioTextoAbajo 
        AddHandler Me.txtMinuto.PreviewTextInput, AddressOf PrevioTextoAbajo
        AddHandler Me.txtSegundo.PreviewTextInput, AddressOf PrevioTextoAbajo

        
    End Sub


End Class
